//
// Created by andryu on 2025/1/4.
//
#include "CubeRenderer.h"
#include "ShapeRenderer.h"
#include "../utils/ShaderLoader.h"
#include "../include/glm/glm.hpp"
#include "../include/glm/gtc/matrix_transform.hpp" // 包含了如 translate, rotate, scale, perspective 等函数
#include "../include/glm/gtc/type_ptr.hpp"
#include <android/log.h>
#include <string>


#define LOG_TAG "LabelSimple"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

GLfloat vertices[] = {
        // 前面
        -0.5f, -0.5f,  0.5f,1.0f, 0.0f, 0.0f,
        0.5f, -0.5f,  0.5f,1.0f, 0.0f, 0.0f,
        0.5f,  0.5f,  0.5f,1.0f, 0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,1.0f, 0.0f, 0.0f,
        // 后面
        -0.5f, -0.5f, -0.5f,0.0f, 1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,0.0f, 1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,0.0f, 1.0f, 0.0f,
        0.5f, -0.5f, -0.5f,0.0f, 1.0f, 0.0f,
        // 左侧
        -0.5f, -0.5f, -0.5f,0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,0.0f, 0.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,0.0f, 0.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,0.0f, 0.0f, 1.0f,
        // 右侧
        0.5f, -0.5f, -0.5f,1.0f, 1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,1.0f, 1.0f, 0.0f,
        0.5f,  0.5f,  0.5f,1.0f, 1.0f, 0.0f,
        0.5f, -0.5f,  0.5f,1.0f, 1.0f, 0.0f,
        // 顶部
        -0.5f,  0.5f, -0.5f,1.0f, 0.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,1.0f, 0.0f, 1.0f,
        0.5f,  0.5f,  0.5f,1.0f, 0.0f, 1.0f,
        0.5f,  0.5f, -0.5f,1.0f, 0.0f, 1.0f,
        // 底部
        -0.5f, -0.5f, -0.5f,0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f,0.0f, 1.0f, 1.0f,
        0.5f, -0.5f,  0.5f,0.0f, 1.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,0.0f, 1.0f, 1.0f,
};

// 立方体的索引数据
GLushort mIndices[] = {
        0,  1,  2,  2,  3,  0,   // 前面
        4,  5,  6,  6,  7,  4,   // 后面
        8,  9, 10, 10, 11,  8,   // 左侧
        12, 13, 14, 14, 15, 12,  // 右侧
        16, 17, 18, 18, 19, 16,  // 顶部
        20, 21, 22, 22, 23, 20   // 底部
};

GLuint vbo, ibo;
GLuint mvpMatrixLoc;
glm::mat4 model, view, projection, mvp;

//着色器代码
std::string CubeRenderer::getVertexShaderCode() const {
    return R"(#version 300 es
            layout(location = 0) in vec4 vPosition;
            layout(location = 1) in vec4 a_Color;
            uniform mat4 u_MVPMatrix;
            out vec4 v_Color;
            void main() {
                gl_Position =  u_MVPMatrix * vPosition;
                v_Color = a_Color;
            })";
}
std::string CubeRenderer::getFragmentShaderCode() const {
    return    R"(#version 300 es
              precision mediump float;
              in vec4 v_Color;
              out vec4 fragColor;
              void main() {
                  fragColor = v_Color;
              })";
}


void CubeRenderer::onSurfaceCreated() {

    glClearColor(1.0,1,1.0,1.0);
    glClearDepthf(1.0);
    //启用深度测试
    glEnable(GL_DEPTH_TEST);
    //设置深度测试函数为“小于或等于”
    glDepthFunc(GL_LEQUAL);

    // 初始化着色器和程序对象
    GLuint vertexShader = ShaderLoader::loadShader(GL_VERTEX_SHADER, getVertexShaderCode().c_str());
    GLuint fragmentShader = ShaderLoader::loadShader(GL_FRAGMENT_SHADER, getFragmentShaderCode().c_str());

    mProgram = glCreateProgram();
    glAttachShader(mProgram, vertexShader);
    glAttachShader(mProgram, fragmentShader);
    glLinkProgram(mProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mIndices), mIndices, GL_STATIC_DRAW);

    mvpMatrixLoc = glGetUniformLocation(mProgram, "u_MVPMatrix");

    // 设置视角和投影矩阵
    view = glm::lookAt(glm::vec3(3.0f, 3.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
    projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
    model = glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(1.0f, 1.0f, 0.0f));

    mvp = projection * view * model;
}

void CubeRenderer::onSurfaceChanged(jint width, jint height) {
    projection = glm::perspective(glm::radians(90.0f), (float)width / (float)height, 0.1f, 100.0f);
    mvp = projection * view * model;
}

void CubeRenderer::onDrawFrame() {
    // 清屏
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(mProgram);

    // 更新模型旋转
    static float angle = 0.0f;
    angle += 1.0f; // 每帧增加的角度
    model = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 1.0f, 0.0f));
    mvp = projection * view * model;

    // 设置 MVP 矩阵到着色器
    glUniformMatrix4fv(mvpMatrixLoc, 1, GL_FALSE, glm::value_ptr(mvp));

    // 获取属性位置
    GLuint positionLoc = glGetAttribLocation(mProgram, "vPosition");
    GLuint colorLoc = glGetAttribLocation(mProgram, "a_Color");
    LOGD("CubeRenderer  positionLoc = %d ", positionLoc);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(positionLoc);
    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)0);

    glEnableVertexAttribArray(colorLoc);
    glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));

    glUniformMatrix4fv(mvpMatrixLoc, 1, GL_FALSE, glm::value_ptr(mvp));

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, nullptr);

    glDisableVertexAttribArray(positionLoc);
}




